package VirtualMachines;

import java.util.Random;

public class MesureDistanceSensor {
	static private boolean isTurnOn;
	static private int distance;
	
	public MesureDistanceSensor(){
		isTurnOn = true;
		distance = 60;
		
		TurnOn();
	}
	
	static public void TurnOn(){
		new Thread(new Runnable() {
			public void run() {
				try {
					while(isTurnOn){
						getCurrentDistance();
//						System.out.println(showDistanceSensorData());
						Thread.sleep(1000);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}).start();
	}
	
	static public boolean isTurnOn(){
		return isTurnOn;
	}
	
	static public int getCurrentDistance(){
		if(distance < 1){
			distance++;
		}
		else{
			Random random = new Random();
			
			if(random.nextBoolean())
				distance++;
			else
				distance--;
		}
		return distance;
	}
	
	static public String showDistanceSensorData(){
		String result;
		
		result = "distance : " + distance;
		return result;
	}
}
